package org.gx.tool.hptool.javabean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.gx.tool.hptool.ToolBeanEntity;
import org.gx.tool.hptool.convert.FieldTypeConvertScheme;
import org.gx.tool.hptool.helper.AbstractToolHelper;
import org.gx.tool.hptool.pojo.EntitySturct;
import org.gx.tool.hptool.pojo.FieldPorperty;
import org.gx.tool.hptool.pojo.Project;
import org.gx.tool.hptool.processer.DataProcesser;
import org.gx.tool.hptool.processer.XLSFileDataProcesser;

public class JavaBeanFileToolHelper extends AbstractToolHelper<ToolBeanEntity> {

	private final static String MODIFIER_PUBLIC = "public ";
	private final static String MODIFIER_PRIVATE = "private";
	private final static String CLASS = "public class ";  
	private final static String JAVA_ROOT_PATH = "\\src\\main\\java";
	private final static String LEFT_FOUR_SPACE = "    ";
	private final static String BEGIN_BRACE = " {";
	private final static String END_BRACE = "}";
	private final static String LEFT_PARENTHESES = " (";
	private final static String RIGHT_PARENTHESES = ")";
	private final static String SEMICOLON = ";";
	private final static String PACKAGE = "package";
	private final static String CHANG_LINE = "\n";
	private final static String IMPORT = "import ";
	 /**
	  * 解析excel 文件中的数据，生成javaBean  对象
	  */
	private final static String basePath ;
	static {
        basePath = System.getProperty("user.dir")+JAVA_ROOT_PATH;
    }
	
	private DataProcesser reader ;
	private  FieldTypeConvertScheme beanConvertScheme;
	
	public JavaBeanFileToolHelper (DataProcesser reader , FieldTypeConvertScheme scheme) {
		
		this.reader = reader;
		this.beanConvertScheme = scheme;
		
	}	

	
	
	
	@Override
	public String outText(Project project, FieldTypeConvertScheme convertScheme) {
		return super.outText(project, convertScheme);
	}



	@Override
	public Project getProjectData() {
		
		return reader.readProjectData();
	}


	/**
	 * 创建javabean  文件
	 */
	public void createJavaBeans (Project project) {
		
		if (null != project) {
			Set<EntitySturct> sturcts = project.getSturcts();
			for (EntitySturct sturct: sturcts ) {
				this.createJavaBeanFile(sturct);
			}
					
		}
	}
	
	
	public void createJavaBeanFile (EntitySturct sturct) {
		StringBuffer javaFile = new StringBuffer();
		if (null != sturct) {
			String sturctPackagePath = sturct.getStructPackagePath();
			StringBuffer javaHeader = new StringBuffer();
			StringBuffer javaBody = new StringBuffer();
			StringBuffer fieldStr = new StringBuffer();
			StringBuffer methodStr = new StringBuffer();
			Set <String> imports = new HashSet<String>();
			if (null != sturctPackagePath) {
				javaHeader.append(PACKAGE).append(" ").append(sturctPackagePath).append(SEMICOLON).append(CHANG_LINE);
				javaHeader.append(CHANG_LINE);
			}
			
			String className = 	sturct.getSturctName();
			javaBody.append(CLASS).append(className);
			javaBody.append(BEGIN_BRACE).append(CHANG_LINE);
			
			
			Set<FieldPorperty> fields = sturct.getFields();
			for (FieldPorperty field : fields) {
				String fieldType = this.beanConvertScheme.getConertType(field.getFieldType());
				if (fieldType .indexOf("." ) > 0) {
					imports.add(fieldType);
					fieldType = fieldType.substring(fieldType.lastIndexOf(".")+1);
				}
				
				fieldStr.append(createPrivateField(field,fieldType)).append(CHANG_LINE);
				methodStr.append(this.createGeterAndSetter(field,fieldType));
			}
			javaBody.append(fieldStr).append(CHANG_LINE).append(methodStr);
			javaBody.append(END_BRACE);
			
			for(String importStr : imports ) {
				javaHeader.append(IMPORT).append(importStr).append(SEMICOLON).append(CHANG_LINE);
			}
			javaFile.append(javaHeader).append(CHANG_LINE).append(javaBody);
			javaFile.append(CHANG_LINE);
			//System.out.println(javaFile.toString());
			this.createFile(sturctPackagePath,sturct.getSturctName(),javaFile.toString());
			
		}
	}
	
	
	private void createFile (String path, String fileName,String content) {
		System.out.println("ori path ; " + path);
		FileOutputStream out = null;
		path = path.replaceAll("[.]", "/");
		
		System.out.println(path);
		String realPath = basePath +"\\"+ path;
		try {
			
			File filePath = new File(realPath);
			if (!filePath.exists() ) {
				filePath.mkdirs();
			}
			
			System.out.println("re : " + realPath + "\\"+fileName+".java");
			File file = new File(realPath + "\\"+fileName+".java");
			if (!file.exists()) {
				
				out = new FileOutputStream(file,true);
				file.createNewFile();
				out.write(content.getBytes("utf-8"));
				
			}
		} catch (IOException e) {
			System.out.println(path + " create faild !");
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private String createPrivateField(FieldPorperty field,String fieldType) {
		
		String localField = LEFT_FOUR_SPACE + MODIFIER_PRIVATE + " " + fieldType + " "  + field.getField() + SEMICOLON;
		return localField;
	}

	private String createGeterAndSetter (FieldPorperty field,String fieldType) {
		StringBuffer localMethod = new StringBuffer();
		String firstChar = field.getField().substring(0, 1).toUpperCase();
		
		localMethod.append(LEFT_FOUR_SPACE).append(MODIFIER_PRIVATE).append(" ");
		localMethod.append(fieldType).append(" get").append(firstChar).append(field.getField().substring(1));
		localMethod.append(LEFT_PARENTHESES);
		localMethod.append(RIGHT_PARENTHESES).append(BEGIN_BRACE).append(CHANG_LINE);
		localMethod.append(LEFT_FOUR_SPACE).append(LEFT_FOUR_SPACE).append("return ").append(" ")
		.append("this.").append(field.getField()).append(SEMICOLON).append(CHANG_LINE);
		localMethod.append(LEFT_FOUR_SPACE).append(END_BRACE);
		
		localMethod.append(CHANG_LINE).append(CHANG_LINE);

		localMethod.append(LEFT_FOUR_SPACE).append(MODIFIER_PRIVATE).append(" ");
		localMethod.append(" void ").append(" set").append(firstChar).append(field.getField().substring(1));
		localMethod.append(LEFT_PARENTHESES);
		localMethod.append(fieldType).append(" ").append(field.getField());
		localMethod.append(RIGHT_PARENTHESES).append(BEGIN_BRACE).append(CHANG_LINE);
		localMethod.append(LEFT_FOUR_SPACE).append(LEFT_FOUR_SPACE).append("this.").append(field.getField()).append(" = ").append(field.getField());
		localMethod.append(SEMICOLON).append(CHANG_LINE);
		localMethod.append(LEFT_FOUR_SPACE).append(END_BRACE);
		localMethod.append(CHANG_LINE).append(CHANG_LINE);
		
		return localMethod.toString();
	}
	
	
	
	
	
	
	
	@Override
	public EntitySturct getEntitySturctData() {
		// TODO Auto-generated method stub
		return super.getEntitySturctData();
	}



	@Override
	public FieldPorperty getFieldData() {
		// TODO Auto-generated method stub 
		return super.getFieldData();
	}

	
}